package com.huluobo.a2107mvpimdemo.mine.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.huluobo.a2107mvpimdemo.R;
import com.huluobo.a2107mvpimdemo.base.BaseFragment;
import com.huluobo.a2107mvpimdemo.login.view.LoginActivity;
import com.huluobo.a2107mvpimdemo.mine.presenter.MineContract;
import com.huluobo.a2107mvpimdemo.mine.presenter.MinePresenter;
import com.hyphenate.chat.EMClient;
import com.uuzuche.lib_zxing.activity.CodeUtils;

public class MineFragment extends BaseFragment<MineContract.View, MinePresenter> implements MineContract.View {
    private Button logout;

    private ImageView qrCode;

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        presenter = new MinePresenter();
    }

    @Override
    protected void initView() {
        logout = findViewById(R.id.mine_logout);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.logout();
            }
        });
        String username = String.format(getString(R.string.logout), EMClient.getInstance().getCurrentUser());
        logout.setText(username);

        qrCode = findViewById(R.id.mine_qrCode);
        String str = EMClient.getInstance().getCurrentUser();

        //二维码中间的logo
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        //生成二维码
        Bitmap image = CodeUtils.createImage(str, 400, 400, bitmap);
        qrCode.setImageBitmap(image);
    }


    @Override
    public void onLogoutSuccess() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), "退出登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
    }

    @Override
    public void onLogoutFailed(String error) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), "退出登录失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }
}